package com.yohann.course_service.controller;

import com.yohann.course_service.entity.Course;
import com.yohann.course_service.entity.vo.AnswerVO;
import com.yohann.course_service.entity.vo.CheckResult;
import com.yohann.course_service.entity.vo.CourseInfo;
import com.yohann.course_service.service.CourseService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-10-18
 */
@Api(tags = "课程")
@RestController
@RequestMapping("/course_service/course")
public class CourseController {
    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    /**
     * 根据id查询课程
     *
     * @param id 课程id
     * @return 返回对应分类
     */
    @ApiOperation("根据id查询课程")
    @GetMapping("getCourseInfoById/{id}")
    public Result getCourseInfoById(@ApiParam("课程id") @PathVariable int id) {
        CourseInfo courseInfo = courseService.getCourseInfoById(id);

        return Result.ok().data("item", courseInfo);
    }

    /**
     * 根据id查询课程
     *
     * @param id 课程id
     * @return 返回对应分类
     */
    @ApiOperation("根据id查询课程")
    @GetMapping("getCourse/{id}")
    public Course getCourse(@ApiParam("课程id") @PathVariable int id) {
        return courseService.getById(id);
    }

    /**
     * 分页查询课程
     *
     * @param current 起始位置
     * @param limit   结束位置
     * @param course  查询条件
     * @return 结果集
     */
    @ApiOperation("分页查询课程")
    @PostMapping("getCourse/{current}/{limit}")
    public Result pageQueryCourse(@ApiParam("起始位置") @PathVariable int current,
                                  @ApiParam("结束位置") @PathVariable int limit,
                                  @ApiParam("查询条件") @RequestBody(required = false) Course course) {
        Map<String, Object> map = courseService.pageQueryCourse(current, limit, Objects.isNull(course) ? "" : course.getTitle());

        return Result.ok().data(map);
    }

    /**
     * 添加课程
     *
     * @param course 课程信息
     * @return 添加状态
     */
    @ApiOperation("添加课程")
    @PostMapping("addCourse")
    public Result addCourse(@ApiParam("课程信息") @RequestBody Course course) {
        course.setGmtCreate(new Date());
        courseService.save(course);

        return Result.ok().data("id", course.getId());
    }

    /**
     * 修改课程
     *
     * @param course 课程信息
     * @return 修改状态
     */
    @ApiOperation("修改课程")
    @PostMapping("updateCourse")
    public Result updateCourse(@ApiParam("课程信息") @RequestBody Course course) {
        boolean update = courseService.updateById(course);

        return Result.state(update);
    }

    /**
     * 删除课程
     *
     * @param id 课程id
     * @return 删除状态
     */
    @ApiOperation("删除课程")
    @GetMapping("deleteCourse/{id}")
    public Result deleteCourse(@ApiParam("课程id") @PathVariable int id) {
        boolean remove = courseService.removeById(id);

        return Result.state(remove);
    }

    /**
     * 查询课程
     *
     * @param title 课程名称
     * @return 课程id集合
     */
    @ApiOperation("查询课程")
    @GetMapping("searchCourse/{title}")
    public List<Integer> searchCourse(@ApiParam("课程名称") @PathVariable String title) {
        return courseService.searchCourse(title);
    }

    /**
     * 课程TOP3
     *
     * @return 课程集合
     */
    @ApiOperation("课程TOP3")
    @GetMapping("courseTop")
    public Result getTop3() {
        List<Course> list = courseService.getTop3();
        return Result.ok().data("list", list);
    }

    @ApiOperation("根据课程id批量获取课程信息")
    @PostMapping("getCourseInfoByIds")
    public Result getCourseInfoByIds(@ApiParam("id集合") @RequestBody(required = false) List<Integer> ids) {
        List<CourseInfo> courseInfoByIds = courseService.getCourseInfoByIds(ids);
        return Result.ok().data("list", courseInfoByIds);
    }

    @ApiOperation("检查答案")
    @PostMapping("checkAnswer")
    public Result checkAnswer(@ApiParam("标准答案") @RequestBody AnswerVO answer) {
        List<CheckResult> results = courseService.checkAnswer(answer);
        return Result.ok().data("list", results);
    }
}

